//
//  WiiChoseRepeatViewController.m
//  iFeller
//
//  Created by Wii on 15/12/16.
//  Copyright © 2015年 Wii. All rights reserved.
//

#import "WiiChoseRepeatViewController.h"
#define ButonHW (60)
#define LabelH (30)
#define LabelW (60)
#define SpacingX (40)
#define SCREEN_W (SCREEN_WIDTH - 20)
#define StarX (SCREEN_W - ButonHW * 3 - SpacingX * 3) / 2
#define StarY (SCREEN_HEIGHT / 2 - (ButonHW * 2 + LabelH * 2 + 20)) / 2
typedef NS_ENUM (NSInteger,WiiRepeat){
    WiiRepeatNever = 100     ,
    WiiRepeatThreehours      ,
    WiiRepeatOneDay          ,
    WiiRepeatWeek            ,
    WiiRepeatMonth           ,
    WiiRepeatYear
};

@interface WiiChoseRepeatViewController () <UIViewControllerTransitioningDelegate>
@property (nonatomic , strong) UIView *menuView;
@end

@implementation WiiChoseRepeatViewController

#pragma mark - init 
- (instancetype)init{
    self = [super init];
    if (self) {
        self.transitioningDelegate = self;
        self.modalTransitionStyle = UIModalPresentationCustom;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
}
- (void)initUserInterface{
    self.view.backgroundColor = [UIColor clearColor];
    
    UIView *mainView = [[UIView alloc]initWithFrame:CGRectMake(10, WiiZero, SCREEN_WIDTH - 20, SCREEN_HEIGHT / 2)];
    mainView.backgroundColor = [UIColor whiteColor];
    mainView.layer.shadowOffset = CGSizeMake(10, 10);
    mainView.layer.shadowRadius = 10;
    mainView.layer.shadowOpacity = 0.5;
    mainView.layer.cornerRadius = 10;
    [self.view addSubview:mainView];
    
    NSInteger tags = 100;
    NSArray *titleArray = @[@"从不",@"每小时",@"每天",@"每周",@"每月",@"每年"];
    NSArray *imageArray = @[@"repeat_never",@"repeat_3hour",@"repeat_day_1",@"repeat_week",@"repeat_month",@"repeat_year"];
    for (int i = 0; i < 2; i ++) {
        for (int j = 0; j < 3; j ++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(StarX + SpacingX / 2 + j * (SpacingX + ButonHW) , StarY + i  * ButonHW * 2, ButonHW , ButonHW);
            [button setBackgroundImage:[UIImage imageNamed:imageArray[j + i * 3]] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(HandleEvent:) forControlEvents:UIControlEventTouchUpInside];
            [button setTag:tags];
            tags ++;
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(StarX + SpacingX / 2 + j * (SpacingX + ButonHW), StarY + ButonHW + i * ButonHW * 2, LabelW, LabelH)];
            [label setText:titleArray[j + i * 3]];
            [label setTextColor:[UIColor blackColor]];
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:12];
            [mainView addSubview:button];
            [mainView addSubview:label];
        }
    }
}
#pragma mark - button HandleEvent
- (void)HandleEvent:(UIButton *)sender{
    if (self.repeatBlock) {
        switch (sender.tag) {
            case WiiRepeatNever:
                self.repeatBlock(@"从不");
                break;
            case WiiRepeatThreehours:
                self.repeatBlock(@"每小时");
                break;
            case WiiRepeatOneDay:
                self.repeatBlock(@"每天");
                break;
            case WiiRepeatWeek:
                self.repeatBlock(@"每个星期");
                break;
            case WiiRepeatMonth:
                self.repeatBlock(@"每一个月");
                break;
            case WiiRepeatYear:
                self.repeatBlock(@"每年");
                break;
            default:
                break;
        }
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - repeatBlock
- (void)respondsButtonWithRepeat:(RepeatBlock)repeatBlock{
    self.repeatBlock = repeatBlock;
}
#pragma mark - UIViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    return [WiiKit transitionWithTransitionType:WiiPresentTransitionPresent];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    return [WiiKit transitionWithTransitionType:WiiPresentTransitionDismiss];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - setter\getter

@end
